﻿using DevelopTools.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace DevelopTools
{
    /// <summary>
    /// 触发控制器：集成此类，重新写射线检测的方法RayCastItemObject()即可
    /// </summary>
    public class DataShowController : MonoBehaviour
    {
        [Header("此脚本位置有具体情况而定，PC时可放置任意active物体上")]
        /// <summary>
        /// 射线检测到的物体信息
        /// </summary>
        public ItemInformation hitItemInfo;

        [HideInInspector]
        public ItemObject item;
        
        [HideInInspector]
        public RaycastHit hit;
        /// <summary>
        /// 检测距离
        /// </summary>
        [HideInInspector]
        public float raycastDistance = 100;
        /// <summary>
        /// 射线检测的方法:换控制器时，重写此方法
        /// </summary>
        /// <returns></returns>
        public virtual GameObject RayCastItemObject()
        {
            //从屏幕端往鼠标点击位置发射射线（VR时重写）
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            GameObject result = null;
            if (Physics.Raycast(ray, out hit, raycastDistance))
            {
                if (!hit.Equals(null))
                {
                    //检测到的物体的数据放入此类
                    hitItemInfo = new ItemInformation(hit.transform.gameObject, hit.point, hit.normal);
                    result = hit.transform.gameObject;
                    item = hit.transform.GetComponent<ItemObject>();
                }
            }
            else
            {
                hitItemInfo = null;
                result = null;
                item = null;
            }
            return result;
        }
        /// <summary>
        /// 打开UI
        /// </summary>
        public virtual void OpenUI()
        {
            RayCastItemObject();
            if (hitItemInfo != null && item != null)
            {
                //通过 获取到的 Item，调用Item中的方法
                item.ShowUI(hitItemInfo.hitPosition, hitItemInfo.hitNormal);
            }
        }
        /// <summary>
        /// 关闭UI
        /// </summary>
        public virtual void CloseUI()
        {
            RayCastItemObject();
            if (hitItemInfo != null && item != null)
            {
                //2017.12.7 新增
                //如果 检测到的与之前打开的不一样，则，关闭检测到的

                //通过 获取到的 Item，调用Item中的方法
                item.HideUI();
            }
        }
    }
    /// <summary>
    /// 检测到的物体信息
    /// </summary>
    [Serializable]
    public class ItemInformation
    {
        public GameObject hitGameObject;
        public Vector3 hitPosition;
        public Vector3 hitNormal;
        public ItemInformation(GameObject gameObject)
        {
            this.hitGameObject = gameObject;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="gameObject">hit.gameObject</param>
        /// <param name="position">hit的位置</param>
        /// <param name="normal">hit.normal</param>
        public ItemInformation(GameObject gameObject, Vector3 position, Vector3 normal)
        {
            this.hitGameObject = gameObject;
            this.hitPosition = position;
            this.hitNormal = normal;
        }
    }
}
